package com.decent.controller;

import com.decent.enums.OrderStatusEnum;
import com.decent.vo.OrderDetailVO;
import com.decent.entity.common.Message;
import com.decent.entity.common.PageResult;
import com.decent.service.OrderService;
import com.decent.util.JwtUtils;
import com.decent.enums.MessageEnum;
import com.github.pagehelper.PageInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author decent
 * @Create 2021/4/8 - 10:32
 * @Description
 */
@RestController
@Validated
public class OrderController {

    @Resource
    private OrderService orderService;

    private  void aa(){
        System.out.println("ssazzxczcs");
    }
    /**
     * 立即下单(计算获取订单信息)
     * @param productId 产品Id
     * @param count 购买数
     * @param token token
     * @return Message
     */
    @RequestMapping("/order/submit")
    public Message orderSubmit(@NotNull(message = "产品id为空") @Min(1) Long productId,
                               @NotNull(message = "数量为空") @Min(1) Integer count,
                               @RequestHeader String token) {
        String uid = JwtUtils.getClaimValueByToken(token, "uid");
        aa();
        return orderService.orderSubmit(productId, count, Integer.parseInt(uid));
    }



    /**
     * 立即支付
     * @param productId 产品Id
     * @param count 购买数
     * @param token token
     * @return Message
     */

    @RequestMapping("/order/pay")
    public Message orderPay(@NotNull(message = "产品id为空") Long productId,
                            @Max(50) @Min(1) Integer count,
                            @RequestHeader String token) {
        String uid = JwtUtils.getClaimValueByToken(token, "uid");
        System.out.println("aaaaaa");
        System.out.println("bbb41");
        System.out.println("bbb41");
        System.out.println("ssss");
        System.out.println("bsssbb41");
        System.out.println("bbb41");
        return orderService.orderPay(productId, count, Integer.parseInt(uid));
    }

    /**
     * 我的订单
     * @param pageNumber 页码
     * @param pageSize 每页数量
     * @param orderStatus 订单状态
     * @param token token
     * @return Message
     */
    @RequestMapping("/order/user/order")
    public Message userOrder(@RequestParam(defaultValue = "1") @Min(1) Integer pageNumber,
                             @RequestParam(defaultValue = "5") @Min(1) Integer pageSize,
                             @RequestParam(defaultValue = "SUCCESS") String orderStatus,
                             @RequestHeader String token) {
        String uid = JwtUtils.getClaimValueByToken(token, "uid");

        if (!OrderStatusEnum.SUCCESS.name().equals(orderStatus)) {
            return new Message(MessageEnum.ORDER_STATUS_PARAM_ERROR);
        }

        PageInfo<OrderDetailVO> pageInfo = orderService.listUserOrder(pageNumber, pageSize, orderStatus, Integer.parseInt(uid));
        PageResult pageResult = new PageResult(pageInfo);
        return new Message(MessageEnum.GENERAL_REQUEST_SUCCESS, pageResult);
    }

    /**
     * 查看卡券(订单产品详情)
     * @param orderId 订单id
     * @param token token
     * @return Message
     */
    @RequestMapping("/order/product/details/{orderId}")
    public Message detailsOrder(@PathVariable("orderId") @NotNull(message = "订单id为空") @Min(1)  Long orderId,
                                @RequestHeader String token) {
        String uid = JwtUtils.getClaimValueByToken(token, "uid");

        List<OrderDetailVO> orderDetailVO = orderService.detailsOrder(orderId, Integer.parseInt(uid));
        return new Message(MessageEnum.GENERAL_REQUEST_SUCCESS, orderDetailVO);
    }



}
